package com.ruoyi.web.controller.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.mapper.SysWarehouseMapper;
import com.ruoyi.web.controller.demo.domain.SysWarehouse;
import com.ruoyi.web.controller.service.ISysWarehouseService;
import com.ruoyi.common.core.text.Convert;

/**
 * sys_warehouseService业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class SysWarehouseServiceImpl implements ISysWarehouseService 
{
    @Autowired
    private SysWarehouseMapper sysWarehouseMapper;

    /**
     * 查询sys_warehouse
     * 
     * @param id sys_warehouse主键
     * @return sys_warehouse
     */
    @Override
    public SysWarehouse selectSysWarehouseById(Long id)
    {
        return sysWarehouseMapper.selectSysWarehouseById(id);
    }

    @Override
    public int updateLine(SysWarehouse sysWarehouse) {
        return sysWarehouseMapper.updateLine(sysWarehouse);
    }

    /**
     * 查询sys_warehouse列表
     * 
     * @param sysWarehouse sys_warehouse
     * @return sys_warehouse
     */
    @Override
    public List<SysWarehouse> selectSysWarehouseList(SysWarehouse sysWarehouse)
    {
        return sysWarehouseMapper.selectSysWarehouseList(sysWarehouse);
    }

    /**
     * 新增sys_warehouse
     * 
     * @param sysWarehouse sys_warehouse
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysWarehouse(SysWarehouse sysWarehouse)
    {
        sysWarehouse.setCreateTime(DateUtils.getNowDate());
        int rows = sysWarehouseMapper.insertSysWarehouse(sysWarehouse);
        insertSysWzxUser(sysWarehouse);
        return rows;
    }

    /**
     * 修改sys_warehouse
     * 
     * @param sysWarehouse sys_warehouse
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysWarehouse(SysWarehouse sysWarehouse)
    {
//        sysWarehouseMapper.deleteSysWzxUserByUserId(sysWarehouse.getId());
        insertSysWzxUser(sysWarehouse);
        return sysWarehouseMapper.updateSysWarehouse(sysWarehouse);
    }

    /**
     * 批量删除sys_warehouse
     * 
     * @param ids 需要删除的sys_warehouse主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysWarehouseByIds(String ids)
    {
//        sysWarehouseMapper.deleteSysWzxUserByUserIds(Convert.toStrArray(ids));
        return sysWarehouseMapper.deleteSysWarehouseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sys_warehouse信息
     * 
     * @param id sys_warehouse主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysWarehouseById(Long id)
    {
//        sysWarehouseMapper.deleteSysWzxUserByUserId(id);
        return sysWarehouseMapper.deleteSysWarehouseById(id);
    }

    /**
     * 新增人员信息信息
     * 
     * @param sysWarehouse sys_warehouse对象
     */
    public void insertSysWzxUser(SysWarehouse sysWarehouse)
    {
        List<SysWzxUser> sysWzxUserList = sysWarehouse.getSysWzxUserList();
        Long id = sysWarehouse.getId();
        if (StringUtils.isNotNull(sysWzxUserList))
        {
            List<SysWzxUser> list = new ArrayList<SysWzxUser>();
            for (SysWzxUser sysWzxUser : sysWzxUserList)
            {
                sysWzxUser.setUserId(id);
                list.add(sysWzxUser);
            }
            if (list.size() > 0)
            {
                sysWarehouseMapper.batchSysWzxUser(list);
            }
        }
    }
}
